import input_data as input_data
import tensorflow as tf


mnist  = input_data.read_data_sets("d:/box/minist/", one_hot=True)

hidden_node = 784
w1 = tf.Variable(tf.ones([784,hidden_node]))
b1 = tf.Variable(tf.ones([1,hidden_node]))

w2 = tf.Variable(tf.ones([hidden_node,10]))
b2 = tf.Variable(tf.ones([1,10]))

x=tf.placeholder("float",[None,784],name="x")
y=tf.placeholder("float",[None,10],name="y")

l1 = tf.nn.relu(tf.matmul(x,w1) + b1)
actv = tf.nn.softmax(tf.matmul(x,w2) + b2)

cost = -tf.reduce_sum(y*tf.log(actv))

optm = tf.train.GradientDescentOptimizer(0.01).minimize(loss = cost)


def validate():
    print(1)

with tf.Session() as sess:

    init = tf.global_variables_initializer()
    sess.run(init)


    for i in range(5000):
        xs,ys = mnist.train.next_batch(100)

        # out = tf.matmul(data.train,w1)+b1

        sess.run(optm,feed_dict={x:xs,y:ys})

        #print(sess.run(cost))
        if i%500 == 0:
            #判断预测标签和实际标签是否匹配

            correct_prediction = tf.equal(tf.argmax(actv,1),tf.argmax(y,1))
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
            #计算所学习到的模型在测试数据集上面的正确率
            print(i, sess.run(accuracy, feed_dict={x:mnist.validation.images, y:mnist.validation.labels}) )